package com.ktwlsoft.archivemanage.controller;


import com.ktwlsoft.archivemanage.domain.business.businessService.InstructionDicService;
import com.ktwlsoft.archivemanage.domain.dao.entity.InstructionDic;
import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.payload.ResultType;
import com.ktwlsoft.archivemanage.payload.instruction.InstrctionDicSaveRequest;
import com.ktwlsoft.archivemanage.payload.instruction.InstrctionDicUpdateRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(value = "指南字典", description = "指南字典")
@RestController
@RequestMapping("/instructiondic/manage")
public class InstructionDicController {
    /**
     * 指南字典
     */
    private final InstructionDicService instructionDicService;

    public InstructionDicController(InstructionDicService instructionDicService) {
        this.instructionDicService = instructionDicService;
    }

    /**
     * 指南字典分页
     *
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "指南字典分页", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping("/list")
    public ResponseEntity<?> list(@RequestParam(defaultValue = "1") int pn,
                                  @RequestParam(defaultValue = "30") int pageSize,
                                  @ApiParam(name = "businessFrom", value = "业务来源") @RequestParam(defaultValue = "", required = true) String businessFrom
    ) {
        return ResponseEntity.ok(instructionDicService.findPage(pn, pageSize, businessFrom));
    }

    /**
     * 指南字典新增
     *
     * @param request
     * @param bindingResult
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "指南字典新增", httpMethod = "POST", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @PostMapping("/add")
    public ResponseEntity<?> add(@Valid @RequestBody InstrctionDicSaveRequest request) {
        Result result = instructionDicService.save(request);
        return ResponseEntity.ok(result);
    }

    /**
     * 指南字典修改
     *
     * @param request
     * @param bindingResult
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "指南字典修改", httpMethod = "POST", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @PostMapping("/edit")
    public ResponseEntity<?> edit(@Valid @RequestBody InstrctionDicUpdateRequest request) {
        Result result = instructionDicService.edit(request);
        return ResponseEntity.ok(result);
    }

    /**
     * 指南字典删除
     *
     * @param id id
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "指南字典删除", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping("/delete")
    public ResponseEntity<?> delete(@ApiParam(name = "id", value = "指南字典id") @RequestParam(defaultValue = "", required = false) Long id) {
        Result result = instructionDicService.deleteById(id);
        return ResponseEntity.ok(result);
    }

    /**
     * 指南导航下的导航列表
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "指南导航下的导航列表")
    @GetMapping("/relatedInstructions")
    public ResponseEntity<?> relatedInstructions(@RequestParam(defaultValue = "1") int pn,
                                                 @RequestParam(defaultValue = "30") int pageSize, Long id) {
        Result result = instructionDicService.getByBelongId(pn, pageSize, id);
        return ResponseEntity.ok(result);
    }

    /**
     * @Description 查询一级指南分页列表
     * @dateTime 2019-12-18 10:54
     * @author wangchong
     */
    @ApiOperation(value = "查询一级指南分页列表")
    @GetMapping("/firstPageList")
    public ResponseEntity<?> firstPageList(@RequestParam Integer pn, @RequestParam Integer pageSize, @ApiParam(name = "businessFrom", value = "业务来源", required = true) @RequestParam InstructionDic.BusinessFrom businessFrom) {
        return ResponseEntity.ok(instructionDicService.getFirstPageList(pn, pageSize, businessFrom));
    }

    /**
     * @Description 根据上级id获取下级列表
     * @dateTime 2019-12-18 11:19
     * @author wangchong
     */
    @ApiOperation(value = "根据上级id获取下级列表")
    @GetMapping("/getChildrenById")
    public ResponseEntity<?> getChildrenById(@RequestParam Long id) {
        return ResponseEntity.ok(instructionDicService.getChildrenById(id));
    }
}
